<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");
header('Content-Type: application/json; charset=utf-8');

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit();
}

require_once '../../utils/Database.php';
require_once '../../utils/Response.php';

$db = new Database();

try {
    // 获取最近7天的日期
    $dates = [];
    $volunteers = [];
    $feedbacks = [];
    
    for ($i = 6; $i >= 0; $i--) {
        $date = date('Y-m-d', strtotime("-{$i} days"));
        $dates[] = date('m-d', strtotime("-{$i} days"));
        
        // 查询当天的讲解员报名数
        $volunteerSql = "SELECT COUNT(*) as count FROM volunteer_applications WHERE DATE(created_at) = ?";
        $volunteerResult = $db->fetch($volunteerSql, [$date]);
        $volunteers[] = intval($volunteerResult['count'] ?? 0);
        
        // 查询当天的意见反馈数
        $feedbackSql = "SELECT COUNT(*) as count FROM feedback WHERE DATE(created_at) = ?";
        $feedbackResult = $db->fetch($feedbackSql, [$date]);
        $feedbacks[] = intval($feedbackResult['count'] ?? 0);
    }
    
    Response::success([
        'dates' => $dates,
        'volunteers' => $volunteers,
        'feedbacks' => $feedbacks
    ]);
    
} catch (Exception $e) {
    Response::error('获取统计数据失败: ' . $e->getMessage());
}
?>